package com.vueblog;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

public class CodeGenerator {

    public static String scanner(String title, boolean notnull) {
        return scanner(title, null, notnull);
    }
    public static String scanner(String title, String example, boolean notnull) {
        Scanner scanner = new Scanner(System.in);
        String tip = "请输入" + title + "：";
        if(example!=null){
            tip += "(例如：" + example + ")";
        }
        System.out.println(tip);
        String str = null;
        if (scanner.hasNextLine()) {
            str = scanner.nextLine();
        }
        if(notnull){
            while (StringUtils.isEmpty(str)) {
                System.out.println(tip);
                if (scanner.hasNextLine()) {
                    str = scanner.nextLine();
                }
            }
        }
        return str;
    }

    public static void main(String[] args) {
        //代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //全局配置
        GlobalConfig config = new GlobalConfig();
        String outputDir = scanner("目录", "X:/xx/project_xxx/src/main/java", true);
        config.setOutputDir(outputDir);
        String author = scanner("作者", false);
        config.setAuthor(author);
        config.setBaseResultMap(true);
        config.setBaseColumnList(true);
        // 是否打开输出目录 默认为true
        config.setOpen(false);
        mpg.setGlobalConfig(config);

        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        String url = scanner("数据源url", "jdbc:oracle:thin:@//127.0.0.1:1521/orcl", true);
        dsc.setUrl(url);
        String driverName = scanner("数据源driverName", "oracle.jdbc.driver.OracleDriver", true);
        dsc.setDriverName(driverName);
        String username = scanner("数据源username", true);
        dsc.setUsername(username);
        String password = scanner("数据源password", true);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        //
        PackageConfig pkg = new PackageConfig();
        String packageName = scanner("包路径", "cn.wuhan.hello", true);
        pkg.setParent(packageName);
        mpg.setPackageInfo(pkg);

        //自定义配置
        // InjectionConfig

        //模板引擎
        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        String[] tables = scanner("表名，多个英文逗号分割", true).split(",");
        //strategy.setTablePrefix("m_");
        strategy.setInclude(tables);

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();

    }


}
